#include <QCoreApplication>
#include <QTextCodec>
#include <QSettings>
#include "mainThread.h"
#include "ethThread.h"
#include "canThread.h"
#include "strategyThread.h"

int main(int argc, char *argv[])
{
    // 创建一个QCoreApplication对象a，用于处理命令行参数
    QCoreApplication a(argc, argv);
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
	QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
	QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));   
#endif
    KgGlobalData::sqlConfig();
    CRTDB *pRTDB = new CRTDB(); // 这里不要写CRTDB(NULL)
    pRTDB->AttachRtdb();
    CBattStack *pBattStack = pRTDB->getStack(0);
    
    QSettings set(DEV_PATH, QSettings::IniFormat);
    if (set.value("Dev/RS485-0", 0).toUInt() == 1)
    {
        MainThread *w = new MainThread(0, pRTDB, 0);
        w->start();
    }
    if (set.value("Dev/RS485-1", 0).toUInt() == 1)
    {
        MainThread *w = new MainThread(0, pRTDB, 1);
        w->start();
    }    
    if (set.value("Dev/RS485-2", 0).toUInt() == 1)
    {
        MainThread *w = new MainThread(0, pRTDB, 2);
        w->start();
    }    
    if (set.value("Dev/RS485-3", 0).toUInt() == 1)
    {
        MainThread *w = new MainThread(0, pRTDB, 3);
        w->start();
    }    
    if (set.value("Dev/RS485-4", 0).toUInt() == 1)
    {
        MainThread *w = new MainThread(0, pRTDB, 4);
        w->start();
    }    
    if (set.value("Dev/CAN-0", 0).toUInt() == 1)
    {
        CanThread *w = new CanThread(0, pRTDB, 0);
        w->start();
    }
    if (set.value("Dev/CAN-1", 0).toUInt() == 1)
    {
        CanThread *w = new CanThread(0, pRTDB, 1);
        w->start();
    }     
    if (set.value("Dev/ETH-0", 0).toUInt() == 1)
    {
        if (set.value("ETH-0/BcmuDevEn", 0).toInt())
        {
            for (int i=0; i<pBattStack->usInstallBankNum; i++)
            {
                EthThread *w = new EthThread(0, pRTDB, 0 + i, 0, 0);
                w->start();
            }
        }  
        for (int i=0; i<set.value("ETH-0/PcsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 0, 1);
            w->start();
        }             
        for (int i=0; i<set.value("ETH-0/UpsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 0, 2);
            w->start();
        }   
        for (int i=0; i<set.value("ETH-0/BmsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 0, 3);
            w->start();
        }    
    }    
    if (set.value("Dev/ETH-1", 0).toUInt() == 1)
    {
        if (set.value("ETH-1/BcmuDevEn", 0).toInt())
        {
            for (int i=0; i<pBattStack->usInstallBankNum; i++)
            {
                EthThread *w = new EthThread(0, pRTDB, 0 + i, 1, 0);
                w->start();
            }
        }     
        for (int i=0; i<set.value("ETH-1/PcsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 1, 1);
            w->start();
        }  
        for (int i=0; i<set.value("ETH-1/UpsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 1, 2);
            w->start();
        }    
        for (int i=0; i<set.value("ETH-1/BmsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 1, 3);
            w->start();
        }                    
    }    
    if (set.value("Dev/ETH-2", 0).toUInt() == 1)
    {
        if (set.value("ETH-2/BcmuDevEn", 0).toInt())
        {
            for (int i=0; i<pBattStack->usInstallBankNum; i++)
            {
                EthThread *w = new EthThread(0, pRTDB, 0 + i, 2, 0);
                w->start();
            }
        }  
        for (int i=0; i<set.value("ETH-2/PcsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 2, 1);
            w->start();
        }          
        for (int i=0; i<set.value("ETH-2/UpsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 2, 2);
            w->start();
        }     
        for (int i=0; i<set.value("ETH-2/BmsDevCount", 0).toInt(); i++)
        {
            EthThread *w = new EthThread(0, pRTDB, 0 + i, 2, 3);
            w->start();
        }      
    }      
    StrategyThread *w = new StrategyThread(0, pRTDB);
    w->start();

    // 执行应用程序
    return a.exec();
}
